var app = getApp();
var util = require('../../utils/md5.js');
var webhost = app.globalData.webhost;
import Notify from '../Vant/notify/notify';
var Login;

Page({
  data: { 
    account: '',
    password: ''
  },

  bindAccount(e){
    this.setData({
      account: e.detail.value
    })
  },

  bindPwd(e) {
    this.setData({
      password: e.detail.value
    })
  },

  Login(){
    var that = this;
    Login();
  },

  onLoad: function (options) {
    var that = this;

    Login = function(){
      var account = that.data.account;
      var password = that.data.password;
      if (account === "" || password === '') {
        Notify({
          text: '账号或密码不能为空！',
          duration: 1500
        }) 
        return false;
      }
      wx.showNavigationBarLoading();
      password = util.hexMD5(password);  
      var data = [{
        String: {
          driverAccount: account,
          password: password
        }
      }] 
      wx.request({
        url: webhost,
        data: {
          eap_username: "driver",
          eap_password: "Driver2018",
          boId: "tspdriver_tsp2DriverServiceBO_bo", //调用的bo
          methodName: 'login', //调用的具体方法
          returnType: "json", //返回参数类型
          parameters: JSON.stringify(data)
        },
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' //默认值
        },
        dataType: 'json',
        complete(){
          wx.hideNavigationBarLoading();       
        },
        success: function ({data}) {  
          if (data.code && data.code == '000000') {
            wx.setStorage({
              key: "xylc_session",
              data: {
                session: data.data
              }
            })
            app.globalData.user = data.data;
            app.globalData.sessionid = data.data.sessionid;
            app.globalData.driverName = data.data.driverName;
            app.globalData.driverAccount = data.data.driverAccount;
            app.globalData.driverCode = data.data.driverCode;
            wx.showToast({
              title: '登录成功！',
            });
            setTimeout(function(){
              wx.switchTab({
                url: '../index/index',
              })
            },1200);
          } else {
            wx.showToast({
              title: data.msg,
              icon: 'none'
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '网络连接失败',
            icon: 'loading'
          })
        }
      })
    }

  },

  onReady: function () {

  },

  onShow: function () {

  },

  onShareAppMessage: function () {

  }
})